package com.nfc.base.DaoImp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.nfc.base.Dao.UserDao;
import com.nfc.base.Entity.User;

@Repository("userDao")
@Transactional
public class UserDaoImp implements UserDao {

	@Autowired
	private SessionFactory sessionFactory;

	public void create(User user) {
		sessionFactory.getCurrentSession().save(user);
	}

	public void update(User user) {
		sessionFactory.getCurrentSession().update(user);

	}

	public User getByID(Integer user_id) {

		Criteria crit = sessionFactory.getCurrentSession().createCriteria(
				User.class);
		crit.add(Restrictions.eq("user_id", user_id));
		List<User> users = crit.list();
		return users.get(0);
	}

	public void delete(Integer user_id) {
		String hql = "delete User where user_id = :id";
		Query q = sessionFactory.getCurrentSession().createQuery(hql).setParameter("id", user_id);
		q.executeUpdate();

	}

	public List<User> getList(User user) {

		Criteria crit = sessionFactory.getCurrentSession().createCriteria(
				User.class);
/*
		if (user.getName() != null) {
			crit.add(Restrictions.like("name", "%" + user.getName() + "%"));

		}

		if (user.getDob() != null) {

			SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd");
			Date dd = user.getDob();

			crit.add(Restrictions.eq("dob", user.getDob()));

		}

		if (user.getJob() != null) {
			crit.add(Restrictions.like("job", "%" + user.getJob() + "%"));

		}*/

		return crit.list();
	}

}
